/*
Package cmd
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2024/4/12 15:30
@File     : cmd.go
@Project  : gohub
@Desc     :
*/
package cmd

import (
	"github.com/spf13/cobra"
	"gohub/pkg/helpers"
	"os"
)

// Env 存储全局选项 --env的值
var Env string

// RegisterGlobalFlags
// @Description: 注册全局选项（flag）
// @param rootCmd
func RegisterGlobalFlags(rootCmd *cobra.Command) {
	rootCmd.PersistentFlags().StringVarP(&Env, "env", "e", "", "load .env file, example: --env=testing will use .env.testing file")
}

// RegisterDefaultCmd
// @Description: 注册默认命令
// @param rootCmd
// @param subCmd
func RegisterDefaultCmd(rootCmd *cobra.Command, subCmd *cobra.Command) {
	cmd, _, err := rootCmd.Find(os.Args[1:])
	firstArg := helpers.FirstElement(os.Args[1:])

	if err == nil && cmd.Use == rootCmd.Use && firstArg != "-h" && firstArg != "--help" {
		args := append([]string{subCmd.Use}, os.Args[1:]...)
		rootCmd.SetArgs(args)
	}
}
